package com.dchh.android.suverynew.views;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dhcc.android.base.common.bean.Action;
import com.dhcc.android.base.common.view.MyPopupWindow;
import com.dhcc.android.base.utils.Constants;

import java.util.List;

/**
 * 层级选择操作
 *
 * @author zhoudan
 * @date 2015年12月30日
 */
public class LevelOperateWindow {

    private MyPopupWindow pw;
    private Activity activity;
    private List<Action> data;

    public LevelOperateWindow(Activity activity, List<Action> data) {
        this.activity = activity;
        this.data = data;
    }

    public void show(View parent, final OnOpClickListener opClickListener) {
        if (pw == null) {
            LinearLayout v = new LinearLayout(activity);
            LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            v.setLayoutParams(ll);
            v.setOrientation(LinearLayout.HORIZONTAL);
            v.setBackgroundResource(R.color.white);

            for (final Action ac : data) {
                LinearLayout op = (LinearLayout) LayoutInflater.from(activity).inflate(R.layout.util_map_select_op_item, null);
                LinearLayout.LayoutParams opll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                op.setLayoutParams(opll);
                TextView tv = (TextView) op.findViewById(R.id.label);
                tv.setText(ac.getLabel());

                op.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        System.out.println("op-key-:" + ac.getKey());
                        if (opClickListener != null) {
                            opClickListener.onOpClick(ac.getKey());
                        }
                    }
                });

                v.addView(op);
            }

            pw = new MyPopupWindow(v, Constants.screenWidth, Constants.screenHight / 12, false);
        }

        pw.showAtLocation(parent, Gravity.BOTTOM, 0, 0);// 显示在最中央的位置
    }


    public void dismiss() {
        if (pw != null) {
            pw.dismiss();
        }
    }

    public interface OnOpClickListener {
        public void onOpClick(String key);
    }
}
